// pages/oilStore/addOilStore.js
import { save } from "../../api/warehouse";
import { memberList } from "../../api/member";
import { isNull } from "../../utils/util";
const phoneRexp = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: [],
    customItem: '全部',
    oilStoreData: {
      areaId: null,
      name: '',
      address: '',
      code: '',
      linkman: '',
      phone: '',
      customerId: null
    },
    supplyName: '',


  },

  bindRegionChange(e) {
    this.data.oilStoreData.areaId = e.detail.code[1]
    this.setData({
      region: e.detail.value,
    })
    console.log(e.detail.code[1]);
  },

  addSupply() {
    wx.navigateTo({
      url: '/pages/product/seller?oilStore=' + true,
    })
  },

  save() {
    let _this = this.data;

    if (isNull(_this.oilStoreData.customerId)) {
      wx.showToast({
        icon: 'none',
        title: '选择供应商',
      })
    } else if (isNull(_this.oilStoreData.areaId)) {
      wx.showToast({
        icon: 'none',
        title: '选择省市',
      })
    } else if (isNull(_this.oilStoreData.name)) {
      wx.showToast({
        icon: 'none',
        title: '输入油库名',
      })
    } else if (isNull(_this.oilStoreData.address)) {
      wx.showToast({
        icon: 'none',
        title: '输入地址',
      })
    } else if (isNull(_this.oilStoreData.code)) {
      wx.showToast({
        icon: 'none',
        title: '输入油库编码',
      })
    } else if (isNull(_this.oilStoreData.linkman)) {
      wx.showToast({
        icon: 'none',
        title: '输入联系人',
      })
    } else if (isNull(_this.oilStoreData.phone)) {
      wx.showToast({
        icon: 'none',
        title: '输入联系号码',
      })
    } else {
      if (!this.isPhone(_this.oilStoreData.phone)) {
        wx.showToast({
          icon: 'none',
          title: '手机号格式错误',
        })
      } else {
        save(_this.oilStoreData).then(res => {
          console.log(res);
          wx.navigateBack()
        })
      }
    }
  },
  isPhone(value) {
    if (!/^1(3|4|5|7|8)\d{9}$/.test(value)) {
      return false
    } else {
      return true
    }
  },

  iptName(e) {
    this.data.oilStoreData.name = e.detail.value
  },
  iptAddress(e) {
    this.data.oilStoreData.address = e.detail.value
  },
  iptOilStoreCode(e) {
    this.data.oilStoreData.code = e.detail.value

  },
  iptOilStoreLinkman(e) {
    this.data.oilStoreData.linkman = e.detail.value

  },
  iptOilStorePhone(e) {
    this.data.oilStoreData.phone = e.detail.value
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页
    if (currPage.data.memberId != undefined) {
      console.log(currPage.data);
      //调取传进的参数
      this.setData({
        [`oilStoreData.customerId`]: currPage.data.memberId,
        supplyName: currPage.data.sellerName
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})